package org.tigr.htc.cmd;

/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

import org.apache.log4j.Logger;
import org.exolab.castor.jdo.TimeStampable;

/**
 * The <b>Task</b> data structure
 *
 */

public class Task implements TimeStampable {
    static Logger log = Logger.getLogger("org.tigr.htc.cmd.Task");
    
    public long cmd;  // created when added to a command
    public int index; // created on start event
    public String machine; // created on start event
    public CmdState state; // last event
    public int worker;  
    public int returnValue;  // finish event 
    public String message;
    private String param; // The param represented by this task
    
    // timestamp
    
    /** The field <code>timeStamp</code>           */
    private long timeStamp;
    
    Task (int index, String state, int returnValue, String message) {
        this.index = index;
        this.returnValue = returnValue;
        this.message = message;
        setState(state);
    }
    
    public Task () {
        if (log.isDebugEnabled()) {
            log.debug("In default constructor.");
        }
    }
    
    /**
     * <code>setState</code> set the command state.
     * 
     * @param p_state
     *            a <code>String</code> value
     */
    public void setState(CmdState p_state) {
        state = p_state;
    }
    
    /**
     * Describe <code>setState</code> method here.
     * 
     * @param p_state
     *            a <code>String</code> value
     */
    public void setState(String p_state) {
        state = CmdState.getState(p_state);
    }
    
    /**
     * <code>getStateAsString</code> used by xml marshaller. returns state as string
     * instead of a CmdState object.
     * 
     * @return a <code>String</code> value
     */
    public String getStateAsString() {
        return state.toString();
    }
    
    public CmdState getState() {
        return state;
    }
    
    public long getCommand() {
        log.debug(" param get command ->" + cmd);
        return cmd;
    }
    
    public void setCommand(long p_cmd) {
        log.debug("param set command " + p_cmd);
        cmd = p_cmd;
    }
    
    // 
    // TimeStampable
    // 
    
    /**
     * The method <code>jdoSetTimeStamp<code>
     *
     *
     * @param p_timeStamp
     *
     */
    public void jdoSetTimeStamp(long p_timeStamp) {
        
        log.debug("CHANGING TIMESTAMP FROM: " + timeStamp + " TO: " + p_timeStamp);
        
        timeStamp = p_timeStamp;
    }
    
    /**
     * The method <code>jdoGetTimeStamp<code>
     *
     *
     * @return
     *
     */
    public long jdoGetTimeStamp() {
        
        log.debug("GRABBING TIMESTAMP " + timeStamp);
        
        return timeStamp;
    }

    public int getReturnValue() {
        return this.returnValue;
    }

    public void setReturnValue(int returnValue) {
        this.returnValue = returnValue;
    }

    public int getIndex() {
        return this.index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
    
    public String getParam() {
        return this.param;
    }

    public void setParam(String param) {
        this.param = param;
    }

    /**
     * The method <code>getMachine</code> returns the value of the
     * property machine
     *
     * @return Returns the machine.
     */
    public String getMachine() {
        return machine;
    }

    /**
     * 
     * The method <code>setMachine</code> sets the value of the
     * property machine
     * 
     * @param machine The machine to set.
     */
    public void setMachine(String machine) {
        this.machine = machine;
    }

    /**
     * The method <code>getWorker</code> returns the value of the
     * property worker
     *
     * @return Returns the worker.
     */
    public int getWorker() {
        return worker;
    }

    /**
     * 
     * The method <code>setWorker</code> sets the value of the
     * property worker
     * 
     * @param worker The worker to set.
     */
    public void setWorker(int worker) {
        this.worker = worker;
    }

    public String toString () {
        return "Index: " + index + " state: " + state + " retVal: " + returnValue + " message '" +
            message + "'" + " param '" + param + "' machine '" + machine + "'";
    }
    
    public Object clone() {
        Task copyTask = new Task();
        
        setAllFields(copyTask);
        
        return copyTask;
    }

    protected void setAllFields(Task p_copy) {
        p_copy.index = index;
        p_copy.machine = machine;
        p_copy.message = message;
        p_copy.state = state;
        p_copy.param = param;
        p_copy.returnValue = returnValue;
        p_copy.worker = worker;
        p_copy.cmd = cmd;      
    }
}
